package com.foreveross.crawl.common.core;

import java.io.Serializable;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.Cacheable;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.OneToMany;
import javax.persistence.Table;

/** 
 * @ClassName: ChannelInfo 
 * @Description: TODO
 * @author luofangyi
 * @date 2014-10-15 下午5:40:27 
 *  
 */ 
@Entity
@Table(name = "CHANNEL_INFO")
@Cacheable
public class ChannelInfoEntity extends DateIntervalEntity {

	/**
	 * 
	 */
	private static final long serialVersionUID = -177263220940192712L;
	
	// Fields
	private Long channelId;
	private String channelName;
	private Set<ProxyChannelRelateEntity> proxyChannelRelates = new HashSet<ProxyChannelRelateEntity>(0);
	
	@Override
	public String[] businessKeys() {
		// TODO Auto-generated method stub
		return new String[]{this.getId().toString()};
	}

	@Column(name = "CHANNEL_ID")
	public Long getChannelId() {
		return this.channelId;
	}

	public void setChannelId(Long channelId) {
		this.channelId = channelId;
	}

	@Column(name = "CHANNEL_NAME")
	public String getChannelName() {
		return this.channelName;
	}

	public void setChannelName(String channelName) {
		this.channelName = channelName;
	}

	@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "channelInfo")
	public Set<ProxyChannelRelateEntity> getProxyChannelRelates() {
		return this.proxyChannelRelates;
	}

	public void setProxyChannelRelates(
			Set<ProxyChannelRelateEntity> proxyChannelRelates) {
		this.proxyChannelRelates = proxyChannelRelates;
	}

	@Override
	public String toString() {
		return "ChannelInfoEntity [channelId=" + channelId + ", channelName="
				+ channelName + ", proxyChannelRelates=" + proxyChannelRelates
				+ "]";
	}

//	@Override
//	public int hashCode() {
//		final int prime = 31;
//		int result = super.hashCode();
//		result = prime * result
//				+ ((channelId == null) ? 0 : channelId.hashCode());
//		result = prime * result
//				+ ((channelName == null) ? 0 : channelName.hashCode());
//		result = prime
//				* result
//				+ ((proxyChannelRelates == null) ? 0 : proxyChannelRelates
//						.hashCode());
//		return result;
//	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (!super.equals(obj))
			return false;
		if (getClass() != obj.getClass())
			return false;
		ChannelInfoEntity other = (ChannelInfoEntity) obj;
		if (channelId == null) {
			if (other.channelId != null)
				return false;
		} else if (!channelId.equals(other.channelId))
			return false;
		if (channelName == null) {
			if (other.channelName != null)
				return false;
		} else if (!channelName.equals(other.channelName))
			return false;
		if (proxyChannelRelates == null) {
			if (other.proxyChannelRelates != null)
				return false;
		} else if (!proxyChannelRelates.equals(other.proxyChannelRelates))
			return false;
		return true;
	}

	public static void deleteAll(){
		getRepository().createJpqlQuery("delete from ChannelInfoEntity").executeUpdate();
	}
	
	public static <T extends org.dayatang.domain.Entity> T load(Class<T> clazz, Serializable id) {
		return getRepository().load(clazz, id);
	}
	
	public static void saveOrUpdate(ChannelInfoEntity entity){
		getRepository().save(entity);
	}
	
	public static boolean isChannelExisted(Long channelId){
		return !getRepository().createJpqlQuery("select t from ChannelInfoEntity t where t.channelId=:channelId").addParameter("channelId", channelId).list().isEmpty();
	}
	
	public static ChannelInfoEntity findChannelInfoByChannelId(Long channelId){
		List<ChannelInfoEntity> list = findByProperty(ChannelInfoEntity.class, "channelId", channelId);
		return (list != null && list.size() != 0) ? list.get(0) : null;
	}

}